package zsc.nine.onlinemall.controller;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import zsc.nine.onlinemall.entity.Admin;
import zsc.nine.onlinemall.service.AdminService;

import java.util.Date;
import java.util.List;

/**
 * @ClassName: AdminController
 * @Description: Controller of Admin operation
 * @Author: Zainus
 * @CreateDate: Jul.23, 2021
 * @UpdateDate: Jul.23, 2021
 * @Version: [v1.0]
 */

/**
 * @类名: AdminController
 * @描述: 管理员用户管理控制类接口
 * @创建人: 叶增钥
 * @创建时间: 2021年7月23日
 * @更新时间: 2021年7月23日
 * @版本: [v1.0]
 */
@RequestMapping("/Admin")
@RestController
public class AdminController {

    @Autowired
    AdminService adminService;

    //添加管理员，注册
    @PostMapping("/Register")
    public String AdminRegister(@RequestParam(value = "AdminEmail", required = false) String AdminEmail,
                                @RequestParam(value = "AdminPassword", required = false) String AdminPassword,
                                @RequestParam(value = "AdminName", required = false) String AdminName) {

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("AdminRegisterResult", "Fail");

        /*后端校验数据合法性*/
        if (AdminEmail == null
                || AdminPassword == null
                || AdminName == null) {
            jsonObject.put("AdminRegisterResult", "Parameter-Null");

        } else if (AdminEmail.trim().isEmpty()
                || AdminPassword.trim().isEmpty()
                || AdminName.trim().isEmpty()) {
            jsonObject.put("AdminRegisterResult", "Empty");

            /*用户邮箱是否已存在*/
        } else if (adminService.AdminExit(AdminEmail)) {
            jsonObject.put("AdminRegisterResult", "Existed");
        } else {

            Admin admin = new Admin();
            admin.setAdminEmail(AdminEmail.trim());
            admin.setAdminName(AdminName.trim());
            admin.setAdminPassword(AdminPassword.trim());
            admin.setRegisterDate(new Date(System.currentTimeMillis()));
            admin.setAdminStatus(1);

            if (adminService.addAdmin(admin)) {
                jsonObject.put("AdminRegisterResult", "Success");
            }

        }


        return jsonObject.toJSONString();
    }


    //管理员登录
    @PostMapping("/Login")
    public String AdminLogin(@RequestParam(value = "AdminEmail", required = false) String AdminEmail,
                             @RequestParam(value = "AdminPassword", required = false) String AdminPassword) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("AdminLoginResult", "Fail");

        /*后端校验数据合法性*/
        if (AdminEmail == null || AdminPassword == null) {
            jsonObject.put("AdminLoginResult", "Parameter-Null");

        } else if (AdminEmail.trim().isEmpty() || AdminPassword.trim().isEmpty()) {
            jsonObject.put("AdminLoginResult", "Empty");

            /*用户邮箱与密码是否匹配*/
        } else if (!adminService.AdminExit(AdminEmail.trim())) {
            jsonObject.put("AdminLoginResult", "Wrong-Email");
        } else {
            Admin admin = adminService.findAdminByEmail(AdminEmail.trim());
            if (admin != null && AdminPassword.equals(admin.getAdminPassword())) {
                jsonObject.put("AdminLoginResult", "Success");
                jsonObject.put("AdminLoginNameResult", admin);
            }
        }


        return jsonObject.toJSONString();
    }


    //更改管理员密码
    @PostMapping("/ResetPassword")
    public String AdminResetPassword(@RequestParam(value = "AdminEmail", required = false) String AdminEmail,
                                     @RequestParam(value = "AdminPassword", required = false) String AdminPassword) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("AdminResetPasswordResult", "Fail");

        /*后端校验数据合法性*/
        if (AdminEmail == null
                || AdminPassword == null) {
            jsonObject.put("AdminResetPasswordResult", "Parameter-Null");

        } else if (AdminEmail.trim().isEmpty()
                || AdminPassword.trim().isEmpty()) {

            jsonObject.put("AdminResetPasswordResult", "Empty");

        } else if (!adminService.AdminExit(AdminEmail.trim())) {
            jsonObject.put("AdminResetPasswordResult", "Wrong-Email");
        } else {

            Admin admin = adminService.findAdminByEmail(AdminEmail);
            if (admin != null) {
                if (AdminPassword.equals(admin.getAdminPassword())) {
                    jsonObject.put("AdminResetPasswordResult", "Identical");
                } else if (adminService.changePasswordByEmail(AdminEmail, AdminPassword)) {
                    jsonObject.put("AdminResetPasswordResult", "Success");
                }
            }

        }
        return jsonObject.toJSONString();
    }


    //更改管理员权限
    @PostMapping("/ResetAuth")
    public String AdminResetAuth(@RequestParam(value = "AdminEmail", required = false) String AdminEmail,
                                 @RequestParam(value = "Status", required = false) String Status) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("AdminResetAuthResult", "Fail");

        /*后端校验数据合法性*/
        if (AdminEmail == null
                || Status == null) {
            jsonObject.put("AdminResetAuthResult", "Parameter-Null");

        } else if (AdminEmail.trim().isEmpty()
                || Status.trim().isEmpty()) {

            jsonObject.put("AdminResetAuthResult", "Empty");

            /*用户邮箱与密码是否匹配*/
        } else if (!adminService.AdminExit(AdminEmail.trim())) {
            jsonObject.put("AdminResetAuthResult", "Wrong-Email");
        } else if (adminService.changeStatus(AdminEmail, Integer.parseInt(Status))) {
            jsonObject.put("AdminResetAuthResult", "Success");
        }

        return jsonObject.toJSONString();
    }


    //展示所有管理员

    /**
     *
     * @return
     */
    @PostMapping("/GetAll")
    public String AdminGetAll() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("AdminGetAllResult", "Fail");


        List<Admin> adminList = adminService.findAdminByEmail();

        if (adminList != null && !adminList.isEmpty()) {
            jsonObject.put("AdminGetAllResult", "Success");
            jsonObject.put("AdminLoginObjectResult", adminList);
        }

        return jsonObject.toJSONString();
    }


    //根据邮箱查找管理员

    /**
     *
     * @param AdminEmail
     * @return
     */
    @PostMapping("/Get")
    public String AdminGet(@RequestParam(value = "AdminEmail", required = false) String AdminEmail) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("AdminGetResult", "Fail");

        /*后端校验数据合法性*/
        if (AdminEmail == null) {
            jsonObject.put("AdminGetResult", "Parameter-Null");

        } else if (AdminEmail.trim().isEmpty()) {

            jsonObject.put("AdminGetResult", "Empty");

        } else {
            Admin admin = adminService.findAdminByEmail(AdminEmail);
            if (admin != null) {
                jsonObject.put("AdminGetResult", "Success");
                jsonObject.put("AdminGetAdminResult", admin);
            }
        }


        return jsonObject.toJSONString();

    }

    /**
     *
     * @param AdminEmail
     * @return
     */
    //删除管理员
    @PostMapping("/Delete")
    public String AdminDelete(@RequestParam(value = "AdminEmail", required = false) String AdminEmail) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("AdminDeleteResult", "Fail");

        /*后端校验数据合法性*/
        if (AdminEmail == null) {
            jsonObject.put("AdminDeleteResult", "Parameter-Null");

        } else if (AdminEmail.trim().isEmpty()) {
            jsonObject.put("AdminDeleteResult", "Empty");

        } else if (!adminService.AdminExit(AdminEmail.trim())) {

            jsonObject.put("AdminDeleteResult", "Wrong-Email");
        } else if (adminService.deleteAdmin(AdminEmail.trim())) {
            jsonObject.put("AdminDeleteResult", "Success");
        }

        return jsonObject.toJSONString();
    }

    /**
     *
     * @param Index
     * @param Size
     * @return
     */
    @PostMapping("/GetList")
    public String AdminGetList(@RequestParam(value = "Index",required = false) String Index,
                               @RequestParam(value = "Size",required = false) String Size) {

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("AdminGetListResult", "Fail");

        /*后端校验数据合法性*/
        if (Index == null
                || Size == null) {

            jsonObject.put("AdminGetListResult", "Parameter-Null");

        } else if (Index.trim().isEmpty()
                || Size.trim().isEmpty()) {
            jsonObject.put("AdminGetListResult", "Empty");
        } else {

            jsonObject.put("AdminGetListResult", "Success");
            jsonObject.put("AdminGetListObjectResult", adminService.pagedFindById(Index, Size));

        }

        return jsonObject.toJSONString();

    }

    /**
     *
     * @return
     */
    @PostMapping("/GetListSize")
    public String AdminGetListSize() {

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("AdminGetListSizeResult", adminService.getLength());

        return jsonObject.toJSONString();

    }


}
